package com.doctcloud.hisinfo.api.factory;

import com.doctcloud.hisinfo.api.RemoteHisOutpBillService;
import com.doctcloud.hisinfo.histable.domain.outpbill.OutpBillItemsDo;
import com.doctcloud.hisinfo.histable.domain.outpbill.OutpOrderDescDo;
import com.doctcloud.hisinfo.histable.domain.outpbill.OutpRcptMasterDo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Component
public class RemoteHisOutpBillFallbackFactory implements FallbackFactory<RemoteHisOutpBillService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteHisOutpBillFallbackFactory.class);
    @Override
    public RemoteHisOutpBillService create(Throwable cause) {
        return new RemoteHisOutpBillService() {
            @Override
            public List<OutpRcptMasterDo> listRcptByDate(Date startDate, Date endDate) {
                // 为这次具体的失败调用记录详细日志，方便排查
                String errorMessage = String.format("获取起始时间:[%s], 结束时间:[%s] 的门诊收费信息失败，服务已降级。", startDate, endDate);
                log.error(errorMessage);

                // 返回一个空的、不可变的列表，这是方案一推荐的做法
                // 调用方收到空列表后，会认为没有数据需要同步，从而自然结束流程
                return Collections.emptyList();
            }

            @Override
            public List<OutpOrderDescDo> listOrdersByRcpt(String rcptNo) {
                String errorMessage = String.format("获取收费单据号:[%s] 的门诊收费信息失败，服务已降级。", rcptNo);
                log.error(errorMessage);
                return Collections.emptyList();
            }

            @Override
            public List<OutpBillItemsDo> listBillByRcpt(String rcptNo) {
                String errorMessage = String.format("获取收费单据号:[%s] 的门诊收费信息失败，服务已降级。", rcptNo);
                log.error(errorMessage);
                return Collections.emptyList();
            }
        };
    }
}
